from utils.network import Interface
from utils import memory
import random


class API(Interface):
    def handle(self, req):
        assert isinstance(req['account'], str)
        user = memory.users[req['account']]
        assert user.password == req['password']
        token = str(hex(random.randint(16 ** 7, 16 ** 8 - 1)))[2:]
        memory.token2account[token] = user.account
        return self.success_respond({
            'token': token
        })
